//
//  ViewController.m
//  BinaryTree
//
//  Created by DeLong Yang on 2017/7/27.
//  Copyright © 2017年 DeLong Yang. All rights reserved.
/*
    博客地址： http://www.cnblogs.com/manji/p/4903990.html
    笔者认为在 swift中应当使用不同方式
 */

#import "ViewController.h"
#import "BinaryTreeNode.h"    // 引入


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    NSArray *values = [NSArray arrayWithObjects:[NSNumber numberWithInteger:4],[NSNumber numberWithInteger:3],[NSNumber numberWithInteger:5],[NSNumber numberWithInteger:2],[NSNumber numberWithInteger:1],[NSNumber numberWithInteger:7],[NSNumber numberWithInteger:8],[NSNumber numberWithInteger:6],[NSNumber numberWithInteger:10], nil];
    
    BinaryTreeNode *tree = [BinaryTreeNode createTreeWithValues:values];
    BinaryTreeNode *node = [BinaryTreeNode treeNodeAtIndex:3 inTree:tree];
    NSLog(@"%@ --- node is:%@",tree,node);
    //
    
    // 
    NSMutableArray *preOrderArray = [NSMutableArray array];
    [BinaryTreeNode preTraverSortTree:tree handler:^(BinaryTreeNode *node) {
        [preOrderArray addObject:@(node.value)];
    }];
    NSLog(@"preOderArray is:%@",preOrderArray);
    
    //
    NSMutableArray *midOrderArray = [NSMutableArray array];
    [BinaryTreeNode midTraverSortTree:tree Handler:^(BinaryTreeNode *node) {
        [midOrderArray addObject:@(node.value)];
    }];
    NSLog(@"midOderArray is:%@",midOrderArray);
    
    // 感觉顺序 有错误我擦。。。。
    NSMutableArray *postOrderArray = [NSMutableArray array];
    [BinaryTreeNode postTraverSortTree:tree Handler:^(BinaryTreeNode *node) {
        [postOrderArray addObject:@(node.value)];
    }];
    NSLog(@"postOderArray is:%@",postOrderArray);
    
    
    //
    NSMutableArray *levelOrderArray = [NSMutableArray array];
    [BinaryTreeNode levelTraversSortTree:tree Handler:^(BinaryTreeNode *node) {
        [levelOrderArray addObject:@(node.value)];
    }];
    
    NSLog(@"levelOderArray is:%@",levelOrderArray);
    
    //
    NSInteger height = [BinaryTreeNode depthOfTree:tree];
    NSLog(@"height is %ld",height);
    
    // 
    NSInteger width = [BinaryTreeNode widthOfTree:tree];
    NSLog(@"width  is %ld",width);
    
    // 我们发现 从 0 - 6 个层 子节点 数量
    NSInteger zeroLevelCount = [BinaryTreeNode numberOfNodesOnLevel:0 inTree:tree];
    NSInteger oneLevelCount = [BinaryTreeNode numberOfNodesOnLevel:1 inTree:tree];
    NSInteger twoLevelCount = [BinaryTreeNode numberOfNodesOnLevel:2 inTree:tree];
    NSInteger threeLevelCount = [BinaryTreeNode numberOfNodesOnLevel:3 inTree:tree];
    NSInteger fourLevelCount = [BinaryTreeNode numberOfNodesOnLevel:4 inTree:tree];
    NSInteger fiveLevelCount = [BinaryTreeNode numberOfNodesOnLevel:5 inTree:tree];
    NSInteger sixLevelCount = [BinaryTreeNode numberOfNodesOnLevel:6 inTree:tree];
  
    //  我们测试叶子结点的数量
    NSInteger leafsNum = [BinaryTreeNode numberOfLeafsInTree:tree];
    
    // 分别使用两种方法 计算 distance
    NSInteger distance = [BinaryTreeNode maxDistanceOfTree:tree];
    TreeNodeProperty *property = [BinaryTreeNode nodePropertyOfTree:tree];
    NSLog(@" %ld",property.distance);
    
    // search node to root node test is fine
    // notice we use address  not value to do this 
    BinaryTreeNode *nodeThree = [BinaryTreeNode treeNodeAtIndex:3 inTree:tree];
    NSArray *rotePath = [BinaryTreeNode pathOfTreeNode:nodeThree InTree:tree];
    
    //  从 结果看 最后一个元素没出来
    BinaryTreeNode *nodeSix = [BinaryTreeNode treeNodeAtIndex:6 inTree:tree];
    BinaryTreeNode *nodeNine = [BinaryTreeNode treeNodeAtIndex:7 inTree:tree];
    BinaryTreeNode *fatherNode = [BinaryTreeNode parentOfNode:nodeSix andNode:nodeNine inTree:tree];
    
    //
    NSArray *path = [BinaryTreeNode pathFromNode:nodeSix toNode:nodeNine inTree:tree];
    NSLog(@"path is %@",path);
    
    NSArray *path2 = [BinaryTreeNode pathFromNode:nodeSix toNode:nodeSix inTree:tree];
    NSLog(@"path2 count is %d",path2.count);
    
    //。笔者认为 应该是 path 的count -1 毕竟是到另一个
    NSInteger dis = [BinaryTreeNode distanceFromNode:nodeSix toNode:nodeNine inTree:tree];
    
    //  nil 
    BinaryTreeNode *invertNode = [BinaryTreeNode invertBinaryTree:tree];
    
    //
    BOOL isFullTree = [BinaryTreeNode isFullBinaryTree:tree];
    BOOL isCompleteTree = [BinaryTreeNode isCompleteBinaryTree:tree];
    
    BinaryTreeNode *fullNode = [BinaryTreeNode createFullNode];
    BinaryTreeNode *comNode = [BinaryTreeNode createCompleteNode];
    
    BOOL fullTwo = [BinaryTreeNode isFullBinaryTree:fullNode];
    BOOL complteTwo = [BinaryTreeNode isCompleteBinaryTree:comNode];
    BOOL fulThree  = [BinaryTreeNode isFullBinaryTree:comNode];
    
    BOOL isBlance = [BinaryTreeNode isAVLBinaryTree:tree];
    BOOL isB2 = [BinaryTreeNode isAVLBinaryTree:comNode];
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
